local L = AceLibrary("AceLocale-2.2"):new("Frosty")

local options = {
	type = 'group',
	args = {}
}

Frosty = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceEvent-2.0", "AceDB-2.0")


Frosty:RegisterChatCommand(L["Slash-Commands"], options)
Frosty:RegisterDB("FrostyDB", "FrostyDBPC")
Frosty:RegisterDefaults('profile', {
	
})

-- ====================
-- VARIABLES
-- ====================
Frosty.frames = {}
Frosty.manager = nil


-- ====================
-- CLASSES
local AceOO = AceLibrary("AceOO-2.0")
-- ====================

-- - - - - - - - - - -
-- DragBar (virtual)
-- - - - - - - - - - -
local DragBar = AceOO.Class()

-- class variables
DragBar.virtual = true
DragBar.width =		200
DragBar.height =	25

-- instance variables
DragBar.prototype.mainFrame = nil

-- instance functions
function DragBar.prototype:CreateFrame(parent)
	local f = CreateFrame("Frame", nil, parent)
	f:SetWidth(DragBar.width)
	f:SetHeight(DragBar.height)
	f:SetPoint("TOPLEFT")
	f:SetBackdrop({
		bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 2, edgeSize = 16,
		insets = { left = 4, right = 4, top = 4, bottom = 4 },
		})
	self.mainFrame = f
end

function DragBar.prototype:ReturnMainFrame()
	if not self.mainFrame then
		Frosty:Print("the Drag Bar you requested has no mainFrame to return")
		return -1
	end
	return self.mainFrame
end

function DragBar.prototype:SetPoint(point, parent, parentPoint, x, y)
	self.mainFrame:ClearAllPoints()
	self.mainFrame:SetPoint(point, parent, parentPoint, x, y)
end

-- - - - - - - - - - -
-- FrostNovaFrame
-- - - - - - - - - - -
local FrostNovaFrame = AceOO.Class(DragBar)

-- instance variables
FrostNovaFrame.prototype.mainFrameText = nil

-- instance functions
function FrostNovaFrame.prototype:CreateFrame(parent)
	FrostNovaFrame.super.prototype.CreateFrame(self, parent)
	self.mainFrameText = self.mainFrame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
	self.mainFrameText:SetPoint("CENTER")
	self.mainFrameText:SetText("Foo")
end


-- - - - - - - - - - -
-- DragBarManager
-- - - - - - - - - - -
local DragBarManager = AceOO.Class()

-- class variables

-- instance variables
DragBarManager.prototype.frames = {}
DragBarManager.prototype.anchorFrame = nil

-- instance functions
function DragBarManager.prototype:init(frame)
	DragBarManager.super.prototype.init(self)
	if not frame then
		Frosty:Print("Error: an anchor frame must be passed when creating a new manager")
	else
		self.anchorFrame = frame
	end
end

function DragBarManager.prototype:AddFrame(frame)
	if not (AceOO.inherits(frame, DragBar)) then
		Frosty:Print("Error: attempt to add a non-DragBar frame to a DragBarManager")
		return
	end
	
	Frosty:Print("Debug: added a frame to a frame manager")
	local numBars = #self.frames
	
	frame:CreateFrame(self.anchorFrame)
	
	if numBars == 0 then -- if this is the first frame
		frame:SetPoint("TOPLEFT", self.anchorFrame, "BOTTOMLEFT", 0, 4)
	else
		frame:SetPoint("TOPLEFT", self.frames[numBars]:ReturnMainFrame(), "BOTTOMLEFT", 0, 4)
	end
	
	table.insert(self.frames, frame)
	
	
end


-- ====================
-- FUNCTIONS
-- ====================

-- ---------------
-- OnEnable
function Frosty:OnEnable()
	-- called when the addon is enabled
	self:Print("Frosty loaded")

	self:CreateMainFrame()
	self.manager = DragBarManager:new(self.frames.mainFrame.frame)
	
	for x = 1, 5, 1 do
		self.manager:AddFrame(FrostNovaFrame:new())
	end
	
end

-- ---------------
-- CreateMainFrame
function Frosty:CreateMainFrame()
	local f = CreateFrame("Frame", nil, UIParent)
	f:SetWidth(100)
	f:SetHeight(50)
	f:SetPoint("CENTER", 200, 0)
	f:SetBackdrop({
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 4, right = 4, top = 4, bottom = 4 },
		})
	self.frames.mainFrame = {frame = f}
end